package com.wf.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.wf.properties.AliOSSProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

/**
 * @author wfyn
 */
@Slf4j
@Component
@EnableConfigurationProperties(AliOSSProperties.class)
public class AliOssUtil {


    private final AliOSSProperties aliOSSProperties;

    public AliOssUtil(AliOSSProperties aliOssProperties) {
        this.aliOSSProperties = aliOssProperties;
    }
    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes, String objectName) {

        OSS ossClient = null;
        try {
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

            // 创建OSSClient实例。
//            ossClient = new OSSClientBuilder()
//                    .build(aliOSSProperties.getEndpoint(),
//                            credentialsProvider);
            ossClient = new OSSClientBuilder()
                    .build(aliOSSProperties.getEndpoint(),
                            aliOSSProperties.getAccessKeyId(),aliOSSProperties.getAccessKeySecret());
            // 创建PutObject请求。
            ossClient.putObject(aliOSSProperties.getBucketName(), objectName, new ByteArrayInputStream(bytes));

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder
                .append(aliOSSProperties.getBucketName())
                .append(".")
                .append(aliOSSProperties.getEndpoint())
                .append("/")
                .append(objectName);

        log.info("文件上传到:{}", stringBuilder);

        return stringBuilder.toString();
    }
}
